package com.gdut.demo.controller;

//import com.alibaba.fastjson.JSONObject;
import com.gdut.demo.message.MessageFormat;
import com.gdut.demo.message.SendHttpRequest;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *
 *
 *
 */
@RestController
public class ReceiveController {

    //文件的存储路径
//    private String uploadPath = "C:/Users/12865/Documents/uploadPath/";
    private String uploadPath = "D:\\0-Jr\\monitor-highcharts\\data\\uploadPath\\";

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     *  接收来自不同用户发送的数据信息并将文件保存到文件系统
     *  存储路径为uploadPath
     *  文件命名规则：yyyyMMdd_hhmmss+上传文件名
     */
    @ResponseBody
    @PostMapping("/upload")
    public Map<String,Object> receiveData(@RequestParam("uploadFile")MultipartFile uploadFile,String userDescription) throws IOException {
        Map<String,Object> map = new HashMap<String, Object>();

        JSONObject userDescriptionJson = new JSONObject(userDescription);
        MessageFormat message = new MessageFormat();
        //解析消息内容
        message.setUser(userDescriptionJson.getString("user"));
        message.setAlgorithm(userDescriptionJson.getString("algorithm"));
        message.setFileExist(userDescriptionJson.getBoolean("fileExist"));
        message.setInfo(userDescriptionJson.getString("info"));

        logger.info("接收到来自"+message.getUser()+"的数据");
        //如果消息中包含文件，则把他们保存在本地，文件名为：yyyyMMdd_hhmmss+原文件名
        if(message.getFileExist()){
            File file = new File(uploadPath+message.getUser());
            if(!file.exists()){
                file.mkdirs();
            }
            String originalFileName = uploadFile.getOriginalFilename();//获取原始文件及扩展名  xxx.扩展名
            Date date = new Date();
            SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd_HHmmss");
            String newFileName = ft.format(date).toString()+originalFileName;
            String newFilePath=uploadPath+message.getUser()+"/"+newFileName; //新文件的路径
            try {
                uploadFile.transferTo(new File(newFilePath));  //将传来的文件写入新建的文件
            }
            catch (IOException e){
                logger.info(e.getMessage()+e.getCause());
                map.put("code","0");  //code:200表示成功，其他表示失败
                map.put("data",null);
                map.put("message",e.getMessage()+" , cause"+e.getCause()); //返回错误信息
            }
        }
        map.put("code","200");  //code:200表示成功，其他表示失败
        //map.put("data",message.getInfo());
        map.put("message","success");
        return map;

    }

    @ResponseBody
    @PostMapping("/testPost")
    public Map<String,Object> testHttp(@RequestBody(required = true)String test){
        Map<String,Object> map = new HashMap<String, Object>();
        JSONObject testObj = new JSONObject(test);
        String user = testObj.getString("user");
        map.put("server",user);
        /*  http://192.168.30.1:10002   是假设的算法服务器的url*/
        SendHttpRequest sendHttpRequest = new SendHttpRequest("http://192.168.30.1:10002",map,"D:\\0-Jr\\monitor-highcharts\\data\\uploadtest.txt");
        sendHttpRequest.doPostTest();
        return map;
    }

//    @PostMapping("/test")
//    public void test(@RequestBody(required = true)String test){
//        Map<String,Object> map = new HashMap<String, Object>();
//        map.put("server","Yu");
//        SendHttpRequest sendHttpRequest = new SendHttpRequest("http://192.168.1.104:10002",map);
//        sendHttpRequest.doPostTest();
//    }
//
//    @ResponseBody
//    @PostMapping("/testSendRequest")
//    public void test2(@RequestBody(required = true)String test){
//        System.out.println("Receive");
//        logger.info("Receive");
//    }


}
